package org.onvivals.admin.module.ums.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.onvivals.common.param.PageParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.iot.entity.IotDevice;
import org.onvivals.module.pms.entity.PmsRole;
import org.onvivals.module.ums.entity.UmsFeedBack;
import org.onvivals.module.ums.entity.UmsUser;
import org.onvivals.module.ums.param.UmsFeedBackParam;
import org.onvivals.module.ums.service.IUmsFeedBackService;
import org.onvivals.module.ums.service.IUmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/ums/feedback")
public class UmsFeedBackController {

	private final IUmsFeedBackService feedBackService;

	private final IUmsUserService umsUserService;

	@Operation(summary = "分页查询全部反馈信息")
	@GetMapping("/page")
	public Result<PageResult<UmsFeedBack>> page(UmsFeedBackParam param) {
		PageParam<UmsFeedBack, UmsFeedBackParam> page = new PageParam<>(param);
		page.setDefaultOrder("create_time desc");
		IPage<UmsFeedBack> pa = feedBackService.page(page, page.getWrapper());
		List<UmsFeedBack> records = pa.getRecords();
		records.forEach(k->{
			if (ObjectUtil.isNotEmpty(k.getUid())){
				UmsUser umsUser = umsUserService.getById(k.getUid());
				if (umsUser!=null){
					k.setUserName(umsUser.getNickname());
				}
			}
		});
		PageResult<UmsFeedBack> pageResult = new PageResult<>(pa.getRecords(), pa.getTotal());
		return Result.OK("查询成功", pageResult);
	}

	@Operation(summary = "查询全部反馈信息")
	@GetMapping()
	public Result<List<UmsFeedBack>> list(UmsFeedBackParam param) {
		PageParam<UmsFeedBack, UmsFeedBackParam> page = new PageParam<>(param);
		List<UmsFeedBack> pmsLlmConfigList = feedBackService.list(page.getOrderWrapper());
		return Result.OK("查询成功", pmsLlmConfigList);
	}

	@Operation(summary = "根据id查询反馈信息")
	@GetMapping("/{id}")
	public Result<UmsFeedBack> get(@PathVariable("id") Long id) {
		UmsFeedBack umsFeedBack = feedBackService.getById(id);
		return Result.OK("查询成功", umsFeedBack);
	}

	@Operation(summary = "添加反馈信息")
	@PostMapping()
	public Result<?> save(@RequestBody UmsFeedBack umsNotice) {
		if (feedBackService.save(umsNotice)) {
			return Result.OK("添加成功");
		}
		return Result.error("添加失败");
	}

	@Operation(summary = "修改反馈信息")
	@PutMapping()
	public Result<?> update(@RequestBody UmsFeedBack umsNotice) {
		if (feedBackService.updateById(umsNotice)) {
			return Result.OK("修改成功");
		}
		return Result.error("修改失败");
	}

	@Operation(summary = "删除反馈信息")
	@DeleteMapping("/{id}")
	public Result<?> remove(@PathVariable("id") Long id) {
		if (feedBackService.removeById(id)) {
			return Result.OK("删除成功");
		}
		return Result.error("删除失败");
	}
}
